package net.sureon.web.system;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.biz.CompanyBO;
import net.sureon.biz.CompanyMgr;
import net.sureon.common.exception.SureonException;
import net.sureon.dao.ConsumeRecord;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class SysConsumeRecord implements Controller {

	private CompanyMgr companyMgr;

	/**
	 * @return the companyMgr
	 */
	public CompanyMgr getCompanyMgr() {
		return companyMgr;
	}

	/**
	 * @param companyMgr
	 *            the companyMgr to set
	 */
	public void setCompanyMgr(CompanyMgr companyMgr) {
		this.companyMgr = companyMgr;
	}

	private static final String VIEW_CONSUME_RECORD = "sysAdmin/sysConsumeRecord";
	private static final String PARAM_VIEW = "view";
	private static final String PARAM_VALUE_BALANCE = "balance";
	private static final String PARAM_ATTR_NAME_RECORD = "records";
	private static final String SITEURL_DORPDOWN_DISPLAY_LIST = "siteURLDropDownDisplayList";

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String inputed = request.getParameter("inputed");
		Map result = new HashMap();
		List records;
		SimpleDateFormat formatToDate = new SimpleDateFormat("yyyy-MM");
		if ("true".equals(inputed)) {
			// 返回某个月分范围内的数据
			String strStartMonth = "";
			String strEndMonth = "";
			long companyID = -1;
			Date startMonth = null;
			Date endMonth = null;
			strStartMonth = request.getParameter("startMonth");
			strEndMonth = request.getParameter("endMonth");
			try {
				companyID = Long.parseLong(request.getParameter("companyID"));
			} catch(Exception e) {
				e.printStackTrace();
			}
			if (companyID == -1) {
				if (!"".equals(strStartMonth) && !"".equals(strEndMonth)) {
					startMonth = formatToDate.parse(strStartMonth);
					endMonth = formatToDate.parse(strEndMonth);
					records = addSiteURLToConsumeRecord(consumeRecordInRangeMonth(startMonth, endMonth));
				}
				else if ((strStartMonth == null || "".equals(strStartMonth))
						|| (strEndMonth == null || "".equals(strEndMonth))) {
					// 列出全部的数据
					records = addSiteURLToConsumeRecord(allConsumeRecord());
				}
				else if ((!"".equals(strStartMonth))
						&& (strEndMonth == null || "".equals(strEndMonth))) {
					endMonth = new Date();
					startMonth = formatToDate.parse(strStartMonth);
					records = addSiteURLToConsumeRecord(consumeRecordInRangeMonth(startMonth, endMonth));
				} else {
					records = new ArrayList();
				}
			} else {
				// 选择了companyID
				if (!"".equals(strStartMonth) && !"".equals(strEndMonth)) {
					startMonth = formatToDate.parse(strStartMonth);
					endMonth = formatToDate.parse(strEndMonth);
					records = addSiteURLToConsumeRecord(consumeRecordByCompanyIDAndMonth(startMonth, endMonth, companyID));
				}
				else if ((strStartMonth == null || "".equals(strStartMonth))
						|| (strEndMonth == null || "".equals(strEndMonth))) {
					// 列出该公司全部的数据
					records = addSiteURLToConsumeRecord(consumeRecordByCompanyID(companyID));
				}
				else if ((!"".equals(strStartMonth))
						&& (strEndMonth == null || "".equals(strEndMonth))) {
					endMonth = new Date();
					startMonth = formatToDate.parse(strStartMonth);
					records = addSiteURLToConsumeRecord(consumeRecordByCompanyIDAndMonth(startMonth, endMonth, companyID));
				} else {
					records = new ArrayList();
				}
			}
			request.setAttribute("companyID", companyID);
			request.setAttribute("strStartMonth", strStartMonth);
			request.setAttribute("strEndMonth", strEndMonth);
		} else {
			records = addSiteURLToConsumeRecord(allConsumeRecord());
		}
		
		
		result.put(SITEURL_DORPDOWN_DISPLAY_LIST, buildSiteURL());
		result.put(PARAM_ATTR_NAME_RECORD, records);
		return new ModelAndView(VIEW_CONSUME_RECORD, result);
	}

	private List allConsumeRecord() throws SureonException {
		return companyMgr.getAllCompanyRecentTenConsumeRecord();
	}

	private List<ConsumeRecord> consumeRecordInRangeMonth(Date startMonth, Date endMonth)
			throws SureonException {
		return companyMgr.getRecentTenConsumeRecordInRangeMonth(startMonth,
				endMonth);
	}

	private List<ConsumeRecord> consumeRecordByCompanyIDAndMonth(Date startMonth,
			Date endMonth, long companyID) {
		return companyMgr.getConsumeRecordBySiteURLConsumeMonth(startMonth,
				endMonth, companyID);
	}

	private List<ConsumeRecord> consumeRecordByCompanyID(long companyID)
			throws SureonException {
		return companyMgr.getRecentTenConsumeRecord(companyID);
	}

	private List<ConsumeRecord> addSiteURLToConsumeRecord(List<ConsumeRecord> list) throws SureonException {
		CompanyBO company = null;
		for(ConsumeRecord record : list) {
			long companyID = record.getCompanyID();
			company = companyMgr.getCompany(companyID);
			record.setCompanyFullName(company.getCompanyVO().getCompanyFullName());
			record.setSiteURL(company.getCompanyVO().getSiteURL());
		}
		return list;
	}
	
	private List<CompanyBO> buildSiteURL() throws SureonException {
		return companyMgr.getAllCompanies();
	}

//	private List<SiteURLDropDownDisplay> buildSiteURLListResult()
//			throws SureonException {
//		List<SiteURLDropDownDisplay> list = new ArrayList<SiteURLDropDownDisplay>();
//		List<CompanyBO> companyBOs = companyMgr.getAllCompanies();
//		for (CompanyBO company : companyBOs) {
//			list.add(new SiteURLDropDownDisplay(company.getCompanyID(), company
//					.getCompanyVO().getSiteURL()));
//		}
//		return list;
//	}

//	static class SiteURLDropDownDisplay {
//		private long companyID;
//		private String siteURL;
//
//		private SiteURLDropDownDisplay(long companyID, String siteURL) {
//			this.companyID = companyID;
//			this.siteURL = siteURL;
//		}
//
//		public long getCompanyID() {
//			return companyID;
//		}
//
//		public void setCompanyID(long companyID) {
//			this.companyID = companyID;
//		}
//
//		public String getSiteURL() {
//			return siteURL;
//		}
//
//		public void setSiteURL(String siteURL) {
//			this.siteURL = siteURL;
//		}
//	}
}
